package bluejack.game.gal;
import nme.display.DisplayObjectContainer;
import nme.errors.Error;
import nme.events.Event;
import nme.events.IOErrorEvent;
import nme.Lib;
import nme.net.URLLoader;
import nme.net.URLRequest;

/**
 * ...
 * @author BlueJack
 */

class GALFacde 
{
	public var control:GALControl;
	public var view:GALView;

	public function new(mainSprite:DisplayObjectContainer) {
		view = new GALView(this);
		mainSprite.addChild(view);
		control = new GALControl(this);
	}
	
	public function loadScript(path:String):Void {
		var loader:URLLoader = new URLLoader();
		loader.addEventListener(Event.COMPLETE, onScriptLoaded);
		loader.addEventListener(IOErrorEvent.IO_ERROR, onScriptLoadFault);
		try {
			loader.load(new URLRequest(path));
		}catch (err:Error) {
			Lib.trace("Error loading requested document: " + path);
		}
	}
	
	private function onScriptLoadFault(e:IOErrorEvent):Void {
		Lib.trace(e);
	}
	
	private function onScriptLoaded(e:Event):Void {
		var str:String = Std.string(e.target.data);
		control.setData(str);
	}
}